package com.fz.song.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.fz.song.pojo.BlogBanner;
import com.fz.song.pojo.BlogLabel;
import com.fz.song.pojo.BlogType;
import com.fz.song.service.IBlogBannerService;
import com.fz.song.util.DateUtil;
import com.fz.song.util.ResultObj;
import com.fz.song.util.UUIDUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("blogbanner")
public class BlogBannerController {


    @Autowired
    private IBlogBannerService blogBannerService;

    @RequestMapping("blog_banner_list_vlew.html")
    public String blog_banner_list_vlew_html() {
        return "admin/blogbanner/blog_banner_list";
    }


    @RequestMapping("blog_banner_list_data")
    public String blog_banner_list_data(HttpServletRequest request, Integer page, @RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {
        //获取数据
        PageInfo<BlogBanner> pageInfo = blogBannerService.getBlogLableListData(page, limit);
        request.setAttribute("pageInfo", pageInfo);
        return "admin/blogbanner/blog_banner_list_data";
    }


    @RequestMapping("updateStatus")
    @ResponseBody
    public Object updateStatus(HttpServletRequest request, String id) {
        BlogBanner blogBanner_db = blogBannerService.getBlogBannerByid(id);
        Integer effective = blogBanner_db.getEffective();
        if (effective == 1) {
            blogBanner_db.setEffective(0);
        } else {
            blogBanner_db.setEffective(1);
        }
        ResultObj resultObj = blogBannerService.updateBlogBanner(blogBanner_db);
        return resultObj;
    }

    @RequestMapping("delBlogBanner")
    @ResponseBody
    public Object delBlogBanner(HttpServletRequest request, String id) {
        ResultObj resultObj = blogBannerService.delBlogBanner(id);
        return resultObj;
    }


    @RequestMapping("go_blogbanner_saveorupdate.html")
    public String go_blogbanner_saveorupdate_html(HttpServletRequest request, String id) {
        if (null != id && !"".equals(id)) {
            BlogBanner blogBanner = blogBannerService.getBlogBannerByid(id);
            request.setAttribute("blogBanner", blogBanner);
        }

        return "admin/blogbanner/blog_banner_save_or_update";
    }

    @RequestMapping("save_blogbanner")
    @ResponseBody
    public Object save_blogbanner(HttpServletRequest request, String jsonstr) {
        BlogBanner blogBanner = JSONObject.parseObject(jsonstr, BlogBanner.class);
        if (blogBanner.getId() == null || "".equals(blogBanner.getId())) {
            blogBanner.setId(UUIDUtil.getUUID());
            blogBanner.setCreatetime(DateUtil.getDateNow());
            blogBanner.setUpdatetime(DateUtil.getDateNow());
            blogBanner.setEffective(0);
            blogBanner.setDel(0);
            return blogBannerService.saveBlogBanner(blogBanner);
        } else {
            BlogBanner blogBanner_db = blogBannerService.getBlogBannerByid(blogBanner.getId());
            blogBanner_db.setUpdatetime(DateUtil.getDateNow());
            blogBanner_db.setBannername(blogBanner.getBannername());
            blogBanner_db.setPicPath(blogBanner.getPicPath());
            return blogBannerService.updateBlogBanner(blogBanner_db);
        }
    }
}
